package springboot_rabbitmq.callback;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author 帅小伙
 * @date 2022/1/6
 * @description   交换机的回响
 */
@Component
@Slf4j
public class MyCallback implements RabbitTemplate.ConfirmCallback  , RabbitTemplate.ReturnCallback {

    /*
     * @Nullable CorrelationData correlationData,   相关性数据
     * boolean ack,           rabbitmq 交换机是否收到信息
     *  @Nullable String cause    未收到数据的原因
     * */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        String id = correlationData!=null ? correlationData.getId() : "";
        if (ack) {
            log.info("交换机已经收到id为消息{}",id);
        } else {
            log.warn("交换机未收到id为{}:消息  原因是:{}",id, cause);
        }
    }


    // 回退方法

    /**
     *
     * @param message   消息
     * @param replyCode
     * @param replyText  退回原因
     * @param exchange  交换机
     * @param routingKey  路由key
     */
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        log.info("消息{},交换机{},退回原因{},路由key{}",new String(message.getBody()),exchange,replyText,routingKey);
    }
}